package com.lovo.csc.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lovo.csc.bean.SupplyBean;
import com.lovo.csc.bean.TenderVendorBean;
import com.lovo.csc.mapper.ISupplyMapper;
import com.lovo.csc.mapper.ITenderVendorMapper;
import com.lovo.csc.service.ITenderVendorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class TenderVendorServiceImpl implements ITenderVendorService {

    @Autowired
    ITenderVendorMapper tenderVendorMapper;
    @Autowired
    ISupplyMapper supplyMapper;

    @Override
    public IPage<TenderVendorBean> findByTId(String tid, int status, Integer pageNO) {

        return tenderVendorMapper.findByItem(new Page<>(pageNO,8),tid, status);
    }

    @Override
    public void updateStatus(long id,int status,String vendorName) {
        //根据id修改状态
        TenderVendorBean bean = tenderVendorMapper.selectById(id);
        bean.setStatus(status);
        tenderVendorMapper.updateById(bean);
        //根据查询出来的bean的投标书编号远程修改状态

        if (status == 1){//中标
            String sidNum = "s"+System.currentTimeMillis();
            //生成供货订单
            SupplyBean supplyBean = new SupplyBean(sidNum,vendorName, bean.getIdNum(), bean.getTenderIdNum() );
            //保存数据库
            supplyMapper.insert(supplyBean);
            //发送mq

        }
    }
    //根据方案编号和选择的供应商生成招标书
    @Override
    public void addtenderVendor(Long idNum, Long[] newSupper) {
        for (int i = 0; i < newSupper.length ; i++) {
            TenderVendorBean tenderVendor = new TenderVendorBean();
            tenderVendor.setTenderIdNum(String.valueOf(idNum));
            tenderVendor.setVendorId(newSupper[i]);
            tenderVendorMapper.insert(tenderVendor);
        }
    }

}
